package com.wanmait.happyheating.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.Admin;
import com.wanmait.happyheating.pojo.AdminType;

import com.wanmait.happyheating.pojo.MaterialsType;
import com.wanmait.happyheating.service.AdminService;
import com.wanmait.happyheating.service.AdminTypeService;
import com.wanmait.happyheating.service.MaterialsTypeService;
import com.wanmait.happyheating.service.WorkersService;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 管理员类型controller
 * @author drago
 * @date 2021-5-31
 */
@RestController
@RequestMapping("/manage/adminType")
public class AdminTypeController {
    @Resource
    private MaterialsTypeService materialsTypeService;
    @Resource
    private AdminTypeService adminTypeService;
    @Resource
    private WorkersService workersService;
    @Resource
    private AdminService adminService;
    /**
     * 查询所有的管理员分类
     */
    @RequestMapping("findAll")
    public Result findAll(){
        return Result.success(adminTypeService.list()) ;
    }

    /**
     *模糊查询
     *
     * @author zhangxiaofeng
     * @since 2021-06-05
     */
    @PostMapping("likeFaultType")
    public Result likeFaultType(@RequestBody AdminType faultType)
    {
        LambdaQueryWrapper<AdminType> adminTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        adminTypeLambdaQueryWrapper.like(AdminType::getTitle,faultType.getTitle());
        List<AdminType> adminTypes=adminTypeService.list(adminTypeLambdaQueryWrapper);
        return Result.success("查询完成",adminTypes);
    }

    /**
     * 查找所有管理员，并根据时间排序。
     */
    @PostMapping("allInfo")
    public Result allInfo(@RequestBody Integer pageNum, Integer pageSize, Integer adminId){
        if(pageNum==null)
        {
            pageNum=1;
        }
        if (adminId==0){
            PageInfo<AdminType> adminTypeServiceByPage=adminTypeService.adminTypeServiceByPage(pageNum,pageSize,adminId);
            return Result.success("查找成功",adminTypeServiceByPage);
        }
        PageInfo<AdminType>adminTypeServiceByPage=adminTypeService.adminTypeServiceByPage(pageNum,pageSize,adminId);

        return Result.success(adminTypeServiceByPage);
    }



     /*
    添加管理员
     */

    @RequestMapping("addAdmin")
    public Result addAdmin(Integer adminTypeId ,String Adminname ,String password,String phone){
        System.out.println("name"+Adminname+"adminTypeId"+adminTypeId+"password"+password+"phone"+phone);
        Admin admin=new Admin();
        UUID uuids = UUID.randomUUID();
        String uuid=uuids+"";
        System.out.println(uuids);
        if(password==null&&password==""){

        }else {
            admin.setPass(password);
        }


        admin.setAdminTypeId(adminTypeId);
        admin.setName(uuid.substring(10,19));
        admin.setUserName("admin");

        admin.setPhone(phone);


        adminService.save(admin);
        return Result.success("添加成功！");

    }



    /**
     * @author zhangxiaofeng
     * @param id 在网址中的参数即为要删除的对象的ID
     */
    @DeleteMapping("deleteAdmin{id:\\d+}")
    public Result deleteOne(@PathVariable Integer id){
        System.out.println("id的值为："+id);
        if ( id != null)
        {
            boolean flag=adminTypeService.removeById(id);
            if (flag){
                return Result.success("删除完成");
            }else {
                return Result.error("删除失败");
            }
        }
        else {
            return Result.error("无效的id");
        }
    }

    /**
     * @author zhangxiofeng 2021-6-6
     * 批量删除
     * @param adminTypeIds List集合，里面存放的时要删除的数组
     */
    @DeleteMapping("deleteBatchAdminType")
    public Result deleteBatch(@RequestBody List<Integer> adminTypeIds){
        adminTypeService.removeByIds(adminTypeIds);
        return Result.success("批量删除成功！");
    }



    /**
     * @author zhangxiaofeng  2021-6-6
     * 修改管理员
     * @param adminType
     */
    @PostMapping("updateAdminType")
    public Result updateAdminType(@RequestBody AdminType adminType){
        if (adminType.getId()!=null)
        {
            boolean flag=adminTypeService.updateById(adminType);
            if (flag){
                return Result.success("修改完成");
            }else {
                return Result.error("修改失败！");
            }
        }
        else {
            return Result.error("无效的id");
        }

    }
    @RequestMapping("findAdminTypeByadminTypeName")
    public Result findAdminTypeByadminTypeName(String adminTypeName){
        LambdaQueryWrapper<AdminType> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(AdminType::getTitle,adminTypeName);
        AdminType adminType= adminTypeService.getOne(wrapper);
        return Result.success(adminType.getId());
    }




}
